function setStorage(key, value) {
  localStorage.setItem(key, value)
}

function getStorage(key, type = 'string') {
  let value = localStorage.getItem(key)
  if (type === 'array') {
    value = value ? value.split(',') : []
  }
  return value
}

// 保存最多9个数组, 并去重
function setLatest9(key, value) {
  let v = getStorage(key) || []
  if (!Array.isArray(v)) {
    v = v.split(',')
  }
  if (v.includes(value)) {
    // 如果数组中存在，删除，并添加到第一个
    v = v.filter(val => val !== value)
  }
  v.unshift(value)
  if (v.length > 9) {
    v.splice(9)
  }
  setStorage(key, v)
}

export { setStorage, getStorage, setLatest9 }
